package com.lzy.webapi.facade;

import com.lzy.common.apis.ParamServiceApi;
import com.lzy.common.constants.CommonConstants;
import com.lzy.common.dtos.ParamBaseDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.member.apis.MemberSaleLogServiceApi;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.member.dtos.MemberSaleLogDto;
import com.lzy.member.dtos.MemberUserDto;
import com.lzy.product.apis.AgentSystemServiceApi;
import com.lzy.product.apis.MemberCouponsServiceApi;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.MemberCouponsDto;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.product.dtos.agent.AgentSystemDto;
import com.lzy.webapi.response.MemberCouponsResponse;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 参数说明服务
 * Created by WIN7 on 2018/1/7.
 */
@Component
public class MemberCouponsFacade {
    public static final Logger LOGGER = LoggerFactory.getLogger(MemberCouponsFacade.class);

    @Autowired
    private ParamServiceApi paramServiceApi;

    @Autowired
    private MemberSaleLogServiceApi memberSaleLogServiceApi;

    @Autowired
    private MemberCouponsServiceApi memberCouponsServiceApi;

    @Autowired
    private MemberUserServiceApi memberUserServiceApi;

    @Autowired
    private ProductUserServiceApi productUserServiceApi;

    @Autowired
    private AgentSystemServiceApi agentSystemServiceApi;

    /**
     * 查询说明
     *
     * @return
     */
    public void setParamValues(MemberCouponsResponse response, Long memberId) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        //卡券金额
        String dragonMoney = CommonConstants.Param.COUPONS_DRAGON_MONEY;
        String availableMoney = CommonConstants.Param.COUPONS_AVAILABLE_MONEY;
        String servicePhone = CommonConstants.Param.SERVICE_PHONE;
        String saleMoney = CommonConstants.Param.SALE_MONEY;
        String availableSaleMoney = CommonConstants.Param.AVAILABLE_SALE_MONEY;

        ParamBaseDto dragonMoneyDto = paramServiceApi.queryByKey(dragonMoney);
        if(dragonMoneyDto != null) {
            response.setDragonMoneyDesc(dragonMoneyDto.getParamValue());
        }

        ParamBaseDto availableMoneyDto = paramServiceApi.queryByKey(availableMoney);
        if(dragonMoneyDto != null) {
            response.setAvailableMoneyDesc(availableMoneyDto.getParamValue());
        }

        ParamBaseDto servicePhoneDto = paramServiceApi.queryByKey(servicePhone);
        if(servicePhoneDto != null) {
            response.setServicePhone(servicePhoneDto.getParamValue());
        }

        ParamBaseDto saleMoneyDto = paramServiceApi.queryByKey(saleMoney);
        if(saleMoneyDto != null) {
            response.setSaleMoneyDesc(saleMoneyDto.getParamValue());
        }

        ParamBaseDto availableSaleMoneyDto = paramServiceApi.queryByKey(availableSaleMoney);
        if(availableSaleMoneyDto != null) {
            response.setAvailableSaleMoneyDesc(availableSaleMoneyDto.getParamValue());
        }

        //总销售金额
        BigDecimal sale = new BigDecimal(0);
        List<MemberSaleLogDto> list = memberSaleLogServiceApi.selectBySaleMemberId(memberId);
        for (MemberSaleLogDto saleLogDto : list){
            sale = sale.add(saleLogDto.getCouponPrice());
        }
        response.setSaleMoney(sale.toString());

        DataSourceHolder.clearDataSource();
        //可提现销售金额
        BigDecimal availableSale = new BigDecimal(0);
        MemberCouponsDto memberCouponsDto1 = memberCouponsServiceApi.findByMemberIdAndType(memberId, BusinessType.MemberCouponsType.COMMISSION.getCode());
        if (memberCouponsDto1!=null) {
            availableSale = availableSale.add(memberCouponsDto1.getAvailableMoney());
        }
        MemberCouponsDto memberCouponsDto2 = memberCouponsServiceApi.findByMemberIdAndType(memberId, BusinessType.MemberCouponsType.COUPONS.getCode());
        if (memberCouponsDto2!=null) {
            availableSale = availableSale.add(memberCouponsDto2.getAvailableMoney());
        }
        response.setAvailableSaleMoney(availableSale.toString());

    }

    /**
     * 获取该代理商体系名称
     * @param memberId
     * @return
     */
    public String getSystemName(Long memberId){
        String systemName="";
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        MemberUserDto userDto = memberUserServiceApi.selectMemberByMemberId(memberId);
        if(userDto != null){
            if(userDto.getAccount() != null){
                DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
                ProductUserDto productUserDto = productUserServiceApi.queryByPhone(userDto.getAccount());
                if(productUserDto != null && productUserDto.getSystemId() != null){
                    AgentSystemDto agentSystemDto = agentSystemServiceApi.findById(productUserDto.getSystemId());
                    if(agentSystemDto != null && agentSystemDto.getShortName() != null && !agentSystemDto.getShortName().equals("")){
                        systemName = agentSystemDto.getShortName();
                    }
                }
                DataSourceHolder.clearDataSource();
            }
        }
        DataSourceHolder.clearDataSource();
        return systemName;
    }
}
